import { NTag } from "naive-ui";
import type { TagProps } from "naive-ui";
import type { BaseComponentType, OmitHTMLAttributes } from "../types";
import { getSlotsDom } from "../utils";

export type TagFactoryConstructorType = {
  props?: TagProps;
  attrs?: OmitHTMLAttributes<"color">;
  defaultSlot?: BaseComponentType;
  avatarSlot?: BaseComponentType;
  IconSlot?: BaseComponentType;
};

export class TagFactory {
  private attrs: OmitHTMLAttributes<"color"> = {};
  private defaultSlot: BaseComponentType = null;
  private props: TagProps = {};
  private avatarSlot: BaseComponentType = null;
  private IconSlot: BaseComponentType = null;
  constructor(data?: TagFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.avatarSlot) this.setAvatar(data.avatarSlot);
    if (data?.IconSlot) this.setIcon(data.IconSlot);
  }
  setAttrs(attrs: OmitHTMLAttributes<"color">) {
    this.attrs = attrs;
    return this;
  }
  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }
  setProps(props: TagProps) {
    this.props = props;
    return this;
  }
  setAvatar(component: BaseComponentType) {
    this.avatarSlot = component;
    return this;
  }

  setIcon(component: BaseComponentType) {
    this.IconSlot = component;
    return this;
  }
  create() {
    return (
      <NTag {...this.props} {...this.attrs}>
        {{
          default: () => getSlotsDom(this.defaultSlot),
          avatar: () => getSlotsDom(this.avatarSlot),
          icon: () => getSlotsDom(this.IconSlot),
        }}
      </NTag>
    );
  }
}
